#include "socket.h"
#include "iomanager.h"

void test_socket(){
    //通过一个网址获取到对应的ip地址
    IPAddress::ptr addr = Address::LookupAnyIPAddress("www.baidu.com");
    if(addr){
        std::cout<<"get address:"<<addr->toString() << std::endl;
    }else{
        std::cout << "get address fail"<< std::endl;
    }
    //创建一个tcp的地址连接
    Socket::ptr sock = Socket::CreateTCP(addr);
    addr->setPort(80);
    std::cout << "addr="<<addr->toString()<<std::endl;
    if(!sock->connect(addr)){
        std::cout << "connect fail" << std::endl;
        return;
    }else{
        std::cout << "connect success:" << addr->toString()<< std::endl;
    }

    const char buffer[] = "GET / HTTP/1.0\r\n\r\n";
    int rt = sock->send(buffer, strlen(buffer));
    if(rt <= 0){
        std::cout << "send fail:"<< rt << std::endl;
    }

    std::string buffs;
    buffs.resize(4096);
    rt = sock->recv(&buffs, buffs.size());
    if(rt <= 0){
        std::cout << "recv fail"<< std::endl;
    }
    buffs.resize(rt);
    std::cout << buffs << std::endl;

}

int main(int argc, char** argv){
    IOManager iom;
    iom.schedule(&test_socket);

    return 0;
}
